package com.pengtu.gsj.service.impl.check.lzgl.overloadMan;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.pengtu.gsj.dao.base.QueryFilter;
import com.pengtu.gsj.dao.lzgl.GovernSpeedDao;
import com.pengtu.gsj.entity.app.Attachment;
import com.pengtu.gsj.entity.check.CheckTable;
import com.pengtu.gsj.entity.lzgl.GovernSpeed;
import com.pengtu.gsj.service.check.CheckTableService;
import com.pengtu.gsj.service.check.lzgl.overloadMan.EnforInfoNetService;
import com.pengtu.gsj.service.system.SystemManageService;
import com.pengtu.gsj.utils.MathsUtils;


@Service
@Transactional
public class EnforInfoNetServiceImpl implements EnforInfoNetService {
	
	@Autowired
	private CheckTableService checkTableService;
	
	@Autowired
	private SystemManageService systemManageService;
	
	@Autowired
	private GovernSpeedDao governSpeedDao;
	
	@Override
	public List<GovernSpeed> getGovernSpeedList(QueryFilter queryFilter){
		return governSpeedDao.find(queryFilter, "testYear", true);
	}
	
	@Override
	public Map<String, Object> getDevelopPlanmMap(String startTime, String endTime, CheckTable checkTable, String tag)throws Exception{
		Map<String, Object> map = new HashMap<String, Object>();
		List<GovernSpeed> lawList = new ArrayList<GovernSpeed>();
		List<Object> value = new ArrayList<Object>();
		List<List<String>> resultLists = new ArrayList<List<String>>();
		List<String> checkResult = null;
		JSONObject jsonObject = JSONObject.parseObject(checkTable.getRetrieval());
		
		//超限信息实现部省站三级联网得分
		double yearFull = Double.parseDouble((String)jsonObject.get("yearFull"));
		//超限信息实现两级联网得分
		double yearPart = Double.parseDouble((String)jsonObject.get("yearPart"));
		//将超限车辆违法信息，及处理信息都录入系统得分
		double poitMark = Double.parseDouble((String)jsonObject.get("poitMark"));
		
		String governSpeedId = (String)jsonObject.get("governSpeedId");
		
		//取到所有文件
		List<Attachment> list = systemManageService.getAllCheckAttachments(startTime, endTime, tag);
		
		double total = yearFull; 						//检查得分
		double percentage = 0; 							//系统完善度
		double tableFull  = yearFull + poitMark*4.0;	//满分
		int years[] = new int[systemManageService.getCheckYear(endTime, startTime, 1)];
		for (int i = 0; i < years.length; i++) {
			years[i] = Integer.parseInt(startTime)+i;
		}
		
		QueryFilter queryFilter = new QueryFilter();
		boolean flag = false;
		if (StringUtils.isEmpty(governSpeedId)) {
			lawList = this.getGovernSpeedList(queryFilter);
		} else {
			value.add(governSpeedId);
			flag = true;
			String[] idArrs = governSpeedId.split(",");
			for (int i = 0; i < idArrs.length; i++) {
				checkResult = Arrays.asList("0");
				GovernSpeed governSpeed = governSpeedDao.get(idArrs[i]);
				
				total += governSpeed.getEnteringPate() >= 80 ? poitMark : 0;
				checkResult.set(0, String.valueOf(governSpeed.getEnteringPate() >= 80 ? poitMark : 0));
				resultLists.add(checkResult);
				lawList.add(governSpeed);
			}
		}
		
		map.put("sum",  MathsUtils.formatDouble(total,1));
		//判断完善度
		percentage = MathsUtils.divDouble(total *100 , tableFull, 2) ;
		checkTableService.updateCheckTable(checkTable, percentage, total, tableFull, value);
		map.put("percent", percentage);
		map.put("years", years);
		map.put("att", list);
		map.put("flag", flag);
		map.put("yearFull", yearFull);
		map.put("lawList", lawList);
		map.put("resultLists", resultLists);
		map.put("score", checkTable.getTestFull());
		map.put("checkTable", checkTable);
		
		return map;
	}
}
